Use case: Input capture in interrupt mode

HAL1

HAL2

Start input capture in interrupt mode

  • The function HAL_TIM_IC_Start_IT(&htim, TIM_CHANNEL_x) is used to enable:

    • The TIMx peripheral.

    • The selected channel x.

    • The capture compare x interrupt.

TIMx IRQ handler

  • Inside the interrupt handler for TIMx, the user calls the global IRQ handler of the TIM driver HAL_TIM_IRQHandler() to handle the capture compare x interrupt request.

Callback function

  • The function HAL_TIM_IC_CaptureCallback() is called when the input capture occurs.

Start input capture in interrupt mode

  • The function HAL_TIM_IC_StartChannel_IT(&htim, TIM_CHANNEL_x) is used to enable:

    1. The selected channel x.

    2. The capture compare x interrupt.

  • The function HAL_TIM_Start() is used to enable and start the TIMx.

TIMx IRQ handler

  • Inside the interrupt handler for TIMx, the user can call either:

    • The global IRQ handler HAL_TIM_IRQHandler() to handle the capture compare x interrupt request, or

    • The single process IRQ handler for specific interrupt events in this case HAL_TIM_CC_IRQHandler().

Callback function

  • The function HAL_TIM_InputCaptureCallback() is called when the input capture event occurs.

Sequence diagram

Input capture

@startuml
skinparam maxMessageSize 440
' To add a number by line
'autonumber

title "Input Capture in HAL 1"

' Fix order of each column
participant "User Application" as App
participant "HAL TIM" as Driver

participant "IT" as IT

group Initialization and configuration of TIMx
App -[hidden]>Driver
end


group Configuration of channel


App -> Driver : \n<b><font color=blue>HAL_TIM_IC_ConfigChannel</b>(channel = <font color=blue>HAL_TIM_CHANNEL_<b>x</b>,\n(TIM_IC_InitTypeDef) {\n.ICPolarity = TIM_ICPOLARITY_RISING,\n.ICSelection = TIM_ICSELECTION_DIRECTTI,\n.ICPrescaler = TIM_ICPSC_DIV1,\n.ICFilter = 0})
Driver --> App : HAL_OK

end

group Start TIMx in IT mode

App -> Driver : HAL_TIM_IC_Start_IT(&htim, HAL_TIM_CHANNEL_<b>x</b>)
Driver --> App : HAL_OK
Driver->Driver : Enable capture compare interrupt\nEnable TIMx counter
Driver --> App : HAL_OK
IT <[#red]-] : <font color=red><b>TIMx IRQ
IT -> Driver : HAL_TIM_IRQHandler()
Driver -> Driver : Clear flag
Driver -> App: HAL_TIM_IC_CaptureCallback()
App --> Driver
Driver --> IT

end
@enduml

Code snippet

HAL1

HAL2

/* Declaration of tim handle */
TIM_HandleTypeDef htim3;

/* Ensure that all required peripherals
(TIM(timer, channel), GPIO, NVIC, etc.) are configured properly
before starting the input capture.*/
.............

/* Start the Input Capture in interrupt mode */
HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_2);

/* User must call the Interrupt Handler
  functions in TIM3 global interrupt */

void TIM3_IRQHandler(void)
{
    /* Code to execute at each interrupt */
    HAL_TIM_IRQHandler(&htim3);
}

/* Call the callback function */

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
      /* Get the Input Capture value */
      uwIC2Value = HAL_TIM_ReadCapturedValue(&htim3, TIM_CHANNEL_2);
}
 /* Declaration of the tim handle */
 hal_tim_handle_t htim3;

 /* Ensure that all required peripherals
 (TIM(timer, channel), GPIO, NVIC, etc.) are configured properly
 before starting the input capture.*/
      .............

/* Start a timer's output channel in interrupt mode. */
 HAL_TIM_IC_StartChannel_IT(&htim3, HAL_TIM_CHANNEL_2);

 /* Start the timer */
 HAL_TIM_Start(&htim3);

 /* User must call the Interrupt Handler
  functions in TIM3 global interrupt */

 void TIM3_IRQHandler(void)
 {
     /* Code to execute at each interrupt */
     HAL_TIM_IRQHandler(&htim3);
 }

 /* Call the callback function */

 void HAL_TIM_InputCaptureCallback(hal_tim_handle_t *htim,
                          hal_tim_channel_t channel);
 {
   /* Get the Input Capture value */
     uwIC2Value = HAL_TIM_IC_ReadChannelCapturedValue(&htim3, HAL_TIM_CHANNEL_2)
 }

Key changes

  1. Input capture start API changes from HAL_TIM_IC_Start_IT() to HAL_TIM_IC_StartChannel_IT() and requires HAL_TIM_Start().

  2. The callback is renamed from HAL_TIM_IC_CaptureCallback() to HAL_TIM_InputCaptureCallback().